package com.gimi.cloud.jimi.manage.controller.app;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.Page;
import com.gimi.cloud.jimi.common.utils.UUIDUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.ColumnProdDTO;
import com.gimi.cloud.jimi.entity.app.ColumnProdEntity;
import com.gimi.cloud.jimi.entity.product.ProductEntity;
import com.gimi.cloud.jimi.manage.service.app.ColumnProdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;

/**
 * @auther loongs
 * @date 2017/12/27  15:28
 */
@RestController
@RequestMapping(value = "columnProd")
public class ColumnProdController {
    private static Logger logger = LoggerFactory.getLogger(ColumnProdController.class);

    private ColumnProdService columnProdService;

    @Autowired
    public ColumnProdController(ColumnProdService columnProdService) {
        this.columnProdService = columnProdService;
    }

    @RequestMapping(value = "/getColumnProdList")
    public void getColumnProdList(@RequestBody ColumnProdDTO columnProdDTO, HttpServletResponse response) {
        logger.info("栏目下的商品管列表(分页)，入参：" + columnProdDTO);
        Response resp = new Response();
        Page<ColumnProdDTO> list = columnProdService.getColumnProdList(columnProdDTO);
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/addColumnProd")
    public void addColumnProd(@RequestBody ColumnProdDTO columnProdDTO, HttpServletResponse response) {
        logger.info("添加栏目下的商品，入参：" + columnProdDTO);
        Response resp = new Response();
        boolean result = columnProdService.addColumnProd(columnProdDTO);
        if (result) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        } else {
            resp.setResponseCode(ResponseCodeEnum.ADD_COLUMNPROD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/addBatchColumnProd")
    public void addBatchColumnProd(@RequestBody ColumnProdDTO columnProdDTO, HttpServletResponse response) {
        logger.info("批量添加栏目下的商品，入参：" + columnProdDTO);
        Response resp = new Response();
        columnProdService.addBatchColumnProd(columnProdDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/goToaddColumnProd")
    public void goToaddColumnProd(@RequestBody ColumnProdDTO columnProdDTO, HttpServletResponse response) {
        logger.info("转跳到添加栏目下的商品，入参：" + columnProdDTO);
        Response resp = new Response();
        List<ProductEntity> productEntities = columnProdService.goToaddColumnProd(columnProdDTO);
        resp.setRes_data(productEntities);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/goToEditColumnProd")
    public void goToEditColumnProd(@RequestBody ColumnProdDTO columnProdDTO, HttpServletResponse response) {
        logger.info("转跳到编辑栏目下的商品，入参：" + columnProdDTO);
        Response resp = new Response();
        ColumnProdEntity columnProdEntity = columnProdService.goToEditColumnProd(columnProdDTO);
        resp.setRes_data(columnProdEntity);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/editColumnProd")
    public void editColumnProd(@RequestBody ColumnProdDTO columnProdDTO, HttpServletResponse response) {
        logger.info("编辑栏目下的商品，入参：" + columnProdDTO);
        Response resp = new Response();
        boolean result = columnProdService.editColumnProd(columnProdDTO);
        if (result) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        } else {
            resp.setResponseCode(ResponseCodeEnum.EDIT_COLUMNPROD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/delColumnProd")
    public void delColumnProd(@RequestBody ColumnProdDTO columnProdDTO, HttpServletResponse response) throws MsgException {
        logger.info("删除栏目下的商品，入参：" + columnProdDTO);
        Response resp = new Response();
        boolean result = columnProdService.delColumnProd(columnProdDTO);
        if (result) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        } else {
            resp.setResponseCode(ResponseCodeEnum.DEL_COLUMNPROD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }


    @RequestMapping(value = "/upLoadColumnProdFile")
    public void upLoadColumnProdFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = "";
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        for (Object entry : multipartFiles.entrySet()) {
            LinkedList<MultipartFile> lists = ((Map.Entry<String, LinkedList<MultipartFile>>) entry).getValue();
            for (MultipartFile file : lists) {
                InputStream is = file.getInputStream();
                String uuId = UUIDUtil.get32UUID();
                String dirPath = "columnProd";
                String[] originalFilenames = file.getOriginalFilename().split("\\.");
                String fileName = uuId + "." + originalFilenames[originalFilenames.length - 1];
                //测试上传时间
                long startTime1 = System.currentTimeMillis();
                String result = AliOSSUtil.uploadFile(is, dirPath, fileName);
                long endTime1 = System.currentTimeMillis();
                logger.info("上传columnProd图片到OSS，所用时间： " + (endTime1 - startTime1) + "ms");

                long startTime2 = System.currentTimeMillis();
                String tempUrl = AliOSSUtil.getFileUrl(result, new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 365 * 100));
                long endTime2 = System.currentTimeMillis();
                logger.info("获得columnProd图片的url路径，所用时间： " + (endTime2 - startTime2) + "ms");

                url += "," + tempUrl;
                System.out.println("==========>上传结果：" + url);
            }
        }

        Response resp = new Response();
        Map<String, String> data = new HashMap<>();
        data.put("url", url.equals("") ? url : url.substring(1));
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
